package com.gome.ocean.service.gddl.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.gome.ocean.common.enums.ProxyType;
import com.gome.ocean.common.exception.ProxyServiceException;
import com.gome.ocean.dao.gddl.ProxyAdapterDAO;
import com.gome.ocean.dao.gddl.ProxyDAO;
import com.gome.ocean.dao.model.gddl.ProxyBO;
import com.gome.ocean.service.gddl.ProxyService;

@Service
@Transactional("transactionManager")
public class ProxyServiceImpl implements ProxyService {

    private static final Logger LOG = LoggerFactory.getLogger(ProxyServiceImpl.class);

    @Autowired
    private ProxyDAO            proxyDAO;
    @Autowired
    private AdapterDelegate     adapterDelegate;

    @Override
    public boolean insertProxyBO(ProxyBO proxyBO) throws ProxyServiceException {
        ProxyBO existedProxy = proxyDAO.findProxyByNameAndCluster(proxyBO.getClusterId(), proxyBO.getName());
        if (null != existedProxy) {
            throw new ProxyServiceException("该集群下已经存在名称为" + proxyBO.getName() + "的代理节点");
        }
        return proxyDAO.insertProxyBO(proxyBO) == 1;
    }

    @Override
    public boolean removeProxyBOById(Long id) {
        return proxyDAO.removeProxyBOById(id) == 1;
    }

    @Override
    public boolean updateProxyBO(ProxyBO proxyBO) throws ProxyServiceException {
        boolean isExist = null != proxyDAO.findProxyBOById(proxyBO.getId());
        if (!isExist) {
            throw new ProxyServiceException("数据库中并不存在此条记录");
        }
        return proxyDAO.updateProxy(proxyBO) == 1;
    }

    @Override
    public ProxyBO findProxyBOById(Long id) {
        return proxyDAO.findProxyBOById(id);
    }

    @Override
    public Page<ProxyBO> findAllProxyBOs(Integer pageNum, Integer pageSize) {
        return (Page) proxyDAO.findAllProxyBOs(new RowBounds(pageNum, pageSize));
    }

    @Override
    public Page<ProxyBO> findAllProxyBOsByProjectNames(List<String> projectNames, Integer pageNum, Integer pageSize) {
        return (Page<ProxyBO>) proxyDAO.findAllProxyBOsByProjectNames(projectNames, new RowBounds(pageNum, pageSize));
    }

    public ProxyAdapterDAO getAccesser(Long proxyId, ProxyType proxyType) {
        final ProxyBO proxyBO = findProxyBOById(proxyId);
        if (proxyBO == null) {
            LOG.error(new StringBuilder("Fail to get proxy information which id = ").append(proxyId).toString());
            return null;
        }
        if (ProxyType.SERVER == proxyType) {
            return adapterDelegate.getProxyNodeAccesser(proxyBO.getHost(),
                proxyBO.getServerPort(),
                proxyBO.getUser(),
                proxyBO.getPassword());
        } else {
            return adapterDelegate.getProxyNodeAccesser(proxyBO.getHost(),
                proxyBO.getManagerPort(),
                proxyBO.getUser(),
                proxyBO.getPassword());
        }
    }

    @Override
    public List<ProxyBO> getProxyList(Long clusterId, Integer status) {
        return proxyDAO.getProxyList(clusterId, status);
    }

    @Override
    public ProxyBO findProxyByName(String name) {
        return proxyDAO.findProxyByName(name);
    }

    @Override
    public List<ProxyBO> findAllProxyByClusterIds(List<Long> clusterIds) {
        try {
            List<ProxyBO> list = proxyDAO.findAllProxyByCluserIds(clusterIds);
            if (list != null && list.size() > 0) {
                return list;
            }
        } catch (Exception e) {
            LOG.error(e.toString());
        }
        return null;
    }

}
